package com.gyh.system.log.utils;

import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author 作者 gyh:
* @version 创建时间：2018年1月8日 下午11:00:27
* ({@Signature(type=Executor.class,method="update", args = { MappedStatement.class,Object.class,ResultHandler.class, CacheKey.class, BoundSql.class})})
*/
@Intercepts({@Signature(type=Executor.class,method="query", args = { MappedStatement.class,Object.class})})
public class MyBatisOperationInterceptor implements Interceptor{
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		Object[] args = invocation.getArgs();
		MappedStatement ms= (MappedStatement) args[0];
		Object parameterObject = args[1] ;
		BoundSql bounSql = ms.getBoundSql(parameterObject);
		String sql = bounSql.getSql();
        logger.info("mybatis 在这里 可以查看  sql语句---->:{}",sql);
        return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		// TODO Auto-generated method stub
//		logger.info("mybatis 拦截器中的 plugin方法");
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {
		// TODO Auto-generated method stub
//		String dialect = properties.getProperty("dialect");
//		logger.info("mybatis 拦截器中添加属性--->:{}",dialect);
		
	}

	
}
